package com.desay.pangoo.itmt.config;

import java.util.List;
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 
 * @Description： 功能描述
 * 
 * @author uidq1163 on [2021年11月29日下午5:34:21]
 * @Modified By： [修改人] on [修改日期] for [修改说明]
 *
 */
@Configuration
@EnableAsync
public class WebConfig implements WebMvcConfigurer {

	/**
	 * 设置访问swagger-ui的拦截器放行
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
		// 这是swagger-ui的
		registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
		// 这是swagger-bootstarp-ui的
		registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
		registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
		WebMvcConfigurer.super.addResourceHandlers(registry);
	}

	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		// 确保Jackson转换器被添加
		converters.add(new MappingJackson2HttpMessageConverter());
	}

	@Bean
	@Primary
	public ThreadPoolTaskExecutor defaultThreadPool() {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		// 核心线程数目
		executor.setCorePoolSize(8);
		// 指定最大线程数
		executor.setMaxPoolSize(64);
		// 队列中最大的数目
		executor.setQueueCapacity(16);
		// 线程名称前缀
		executor.setThreadNamePrefix("dataSetThreadPool_");
		// rejection-policy：当pool已经达到max size的时候，如何处理新任务
		// CALLER_RUNS：不在新线程中执行任务，而是由调用者所在的线程来执行
		// 对拒绝task的处理策略
		executor.setRejectedExecutionHandler(new CallerRunsPolicy());
		executor.setWaitForTasksToCompleteOnShutdown(true);
		// 线程空闲后的最大存活时间
		executor.setKeepAliveSeconds(30);
		// 加载
		executor.initialize();
		return executor;
	}

	@Bean
	public RestTemplate restTemplate() {
		return new RestTemplate();
	}

	@Bean
	public OkHttpClient okHttpClient() {
		return new OkHttpClient.Builder()
				// 设置连接超时时间为 60 秒
				.connectTimeout(60, TimeUnit.SECONDS)
				// 设置读取超时时间为 600 秒
				.readTimeout(600, TimeUnit.SECONDS)
				// 设置写超时时间为 60 秒
				.writeTimeout(60, TimeUnit.SECONDS)
				// 在连接失败时重试
				.retryOnConnectionFailure(true)
				// 跟随 HTTP 重定向
				.followRedirects(true)
				// 跟随 SSL 重定向
				.followSslRedirects(true).build();
	}

	@Override
	public void configureAsyncSupport(AsyncSupportConfigurer configure) {
		configure.setTaskExecutor(defaultThreadPool());
		// 30 seconds
		configure.setDefaultTimeout(3600000);
	}
}